/**
 * Title:        表单中存在相同名字的字段检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.Util;



public class SameFieldNameCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("SameFieldNameCheck  start");
        List resultList = new ArrayList();
        
        String sqlStr = "";
        sqlStr +=" select workflow_billfield.id, HtmlLabelInfo.Labelname, ";
        sqlStr +=" workflow_billfield.fieldname ,t3.tablename ";
        sqlStr +="   from workflow_billfield ";
        sqlStr +="  inner join HtmlLabelInfo ";
        sqlStr +="     on HtmlLabelInfo.Indexid = workflow_billfield.fieldlabel ";
        sqlStr +="    and HtmlLabelInfo.languageid = 7 ";
        sqlStr +="  inner join (select fieldname, tablename ";
        sqlStr +="                from (select count(1) cnt, fieldname, tablename ";
        sqlStr +="                       from (select fieldname, ";
        sqlStr +="                                    case ";
        sqlStr +="                                      when detailtable is null then ";
        sqlStr +="                                       (select tablename ";
        sqlStr +="                                          from workflow_bill ";
        sqlStr +="                                         where workflow_bill.id = ";
        sqlStr +="                                               workflow_billfield.billid) ";
        sqlStr +="                                      else ";
        sqlStr +="                                       detailtable ";
        sqlStr +="                                    end as tablename ";
        sqlStr +="                               from workflow_billfield ";
        sqlStr +="                              where billid < 0) aaa ";
        sqlStr +="                      group by fieldname, tablename) bbb ";
        sqlStr +="              where cnt > 1 ";
        sqlStr +="                and tablename like 'formtable_main%') t3 ";
        sqlStr +="    on t3.fieldname = workflow_billfield.fieldname ";
        sqlStr +="   and (case ";
        sqlStr +="         when workflow_billfield.detailtable is null then ";
        sqlStr +="          (select tablename ";
        sqlStr +="             from workflow_bill ";
        sqlStr +="            where workflow_bill.id = workflow_billfield.billid) ";
        sqlStr +="         else ";
        sqlStr +="          workflow_billfield.detailtable ";
        sqlStr +="       end) = t3.tablename ";
        sqlStr +=" order by t3.tablename,t3.fieldname,workflow_billfield.id ";
        
        recordSet.executeSql(sqlStr);
        int cnt = 0;
        while(recordSet.next()){
            Map resultMap = new HashMap<String,String>();
            int version = recordSet.getInt("version");
            resultMap.put("showMessage",formatMessage(recordSet.getString("tablename"), "存在多个" + recordSet.getString("fieldname") + "。", "name=" + recordSet.getString("labelname") + " id=" + recordSet.getString("id") ));
            resultMap.put("buttonname","删除");
            resultMap.put("billfieldid", recordSet.getString("id"));
            resultList.add(resultMap);
            cnt++;
        }
        String msg = "";
        String result = "";
        if(cnt == 0){
            result = resultOk;
        }else{
            result = resultNeedFix;
        }
        (new BaseBean()).writeLog("SameFieldNameCheck  end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        (new BaseBean()).writeLog("SameFieldNameCheck  start");
        setRecordSet();

        String result = resultOk;
        List resultList = new ArrayList();
        if(dataMap.get("billfieldid") != null && !dataMap.get("billfieldid").equals("")){
            //根据参数获取列表
            String billfieldids = (String)dataMap.get("billfieldid");
            String divids =  (String)dataMap.get("divid");
            List<String> billfieldidList = Util.splitString2List(billfieldids, ",");
            List<String> dividList = Util.splitString2List(divids, ",");
            for(int i = 0; i < billfieldidList.size(); i++){
                String billfieldid = billfieldidList.get(i);
                String divid = dividList.get(i);
                String sqlStr = "";
                sqlStr +=" select count(1) as count ";
                sqlStr +="   from workflow_billfield ";
                sqlStr +="  inner join (select fieldname, ";
                sqlStr +="                     case ";
                sqlStr +="                       when detailtable is null then ";
                sqlStr +="                        (select tablename ";
                sqlStr +="                           from workflow_bill ";
                sqlStr +="                          where workflow_bill.id = workflow_billfield.billid) ";
                sqlStr +="                       else ";
                sqlStr +="                        detailtable ";
                sqlStr +="                     end as tablename ";
                sqlStr +="                from workflow_billfield ";
                sqlStr +="               where billid < 0 ";
                sqlStr +="                 and id = " + billfieldid + ") t3 ";
                sqlStr +="     on t3.fieldname = workflow_billfield.fieldname ";
                sqlStr +="    and (case ";
                sqlStr +="          when workflow_billfield.detailtable is null then ";
                sqlStr +="           (select tablename ";
                sqlStr +="              from workflow_bill ";
                sqlStr +="             where workflow_bill.id = workflow_billfield.billid) ";
                sqlStr +="          else ";
                sqlStr +="           workflow_billfield.detailtable ";
                sqlStr +="        end) = t3.tablename ";
                sqlStr +="  group by t3.tablename, t3.fieldname ";
                sqlStr +="  order by t3.tablename, t3.fieldname ";
                
                recordSet.executeSql(sqlStr);
                Map resultMap = new HashMap<String,String>();
                if(recordSet.next()){
                    if(recordSet.getInt("count") > 1){
                        recordSet.executeUpdate(" delete from workflow_billfield where id =" + billfieldid);
                        resultMap.put("result",resultOk);
                        resultMap.put("divid",divid);
                        resultMap.put("fixMessage","修复成功");
                    }else{
                        resultMap.put("result",resultOk);
                        resultMap.put("divid",divid);
                        resultMap.put("fixMessage","该流程只存在一个该字段，不需要修复");
                    }
                }
                resultList.add(resultMap);
                
            }
            //return formatReturnJsonFix(result, resultList,this);
        }
        needReload = "1";
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "表单中存在相同名字的字段检测(待完善)";
    }

    @Override
    public String getClassDesc() {
        return "查看表单中是否存在相同名字的字段";
    }
}
